//
//  Team9renovateViewController.m
//  Team9Project
//
//  Created by dlios on 15/10/13.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9renovateViewController.h"
#import "Team9RenovateTableViewCell.h"
#import "Team9homeTableViewCell.h"
#import "Team9renovatePushViewController.h"
#import "JQAFNetworkTool.h"
#import "Team9RenovateModel.h"
#import "Team9HomeModel.h"
#import "Team9BaseView.h"
#import <Masonry.h>
#import <UIImageView+WebCache.h>
#import "Team9s_homeViewController.h"
#import <MJRefresh.h>
@interface Team9renovateViewController ()<UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>
@property (nonatomic, strong) UIButton *renovateButton;
@property (nonatomic, strong) UIButton *homeButton;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIView *rightView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UITableView *renovateTableView;
@property (nonatomic, strong) UITableView *homeTableView;
@property (nonatomic, strong) NSMutableArray *renovateArray;
@property (nonatomic, strong) NSMutableArray *homeArray;
@property (nonatomic, strong) Team9EmptyTool *empty2;
@property(nonatomic, assign) BOOL selectRight;
@end

@implementation Team9renovateViewController

- (void)viewWillAppear:(BOOL)animated{
    if ([Team9EveningType shareEvening].eveningFailling) {
        self.renovateTableView.backgroundColor = [UIColor blackColor];
        self.homeTableView.backgroundColor = [UIColor blackColor];
    }else{
        self.renovateTableView.backgroundColor = [UIColor whiteColor];
        self.homeTableView.backgroundColor = [UIColor whiteColor];
    
    }


}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.translucent = NO;

    [self createHeaderView];
    [self createScrollView];
    
    [self createRenovateTableView];
    [self createHomeTableView];
    
    self.empty = [Team9EmptyTool toolWithScrollView:self.renovateTableView];
    self.empty2 = [Team9EmptyTool toolWithScrollView:self.renovateTableView];
    
    [self createRenovateData];
    
    
}

#pragma mark 数据解析
- (void)createRenovateData{
    
 self.tool = [Team9RefreshTool toolWithScrollView:self.renovateTableView url:@"http://api.meilijia.com/app/ba_diary_book_list?type=2&sort=&page=1" style:^NSArray *(id result) {
      NSDictionary *dic = result[@"data"];
      return [Team9RenovateModel baseModelByArr:dic[@"ba_diary_book_list"][@"list"]];
  } refresh:^(NSArray *array) {
      self.renovateArray = array.mutableCopy;
      [self.renovateTableView reloadData];
      [self createHomeData];
  } empty:nil];
    
    
//    [JQAFNetworkTool getUrl:@"http://api.meilijia.com/app/ba_diary_book_list?type=2&sort=&page=1" body:nil result:JQJSON headerFile:nil success:^(id result) {
//       
//        self.renovateArray = [NSMutableArray array];
//        NSDictionary *dic = result[@"data"];
//        self.renovateArray = [Team9RenovateModel baseModelByArr:dic[@"ba_diary_book_list"][@"list"]].mutableCopy;
//        
//        
//        
//        
//        // 触发下拉刷新方法
//        self.renovateTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//            
//            [self loadNewData:self.renovateTableView];
//            
//        }];
//        
//        
//        // 触发上拉加载数据方法
//        self.renovateTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//            
//            [self loadMoreData:self.renovateTableView];
//            
//        }];
//
//        
//        
//        [self.renovateTableView reloadData];
//        [self createHomeData];
//        
//    } failure:^(NSError *error) {
//       
//        
//    }];
    
    
    

}



#pragma mark 刷新和加载
- (void)loadNewData:(UITableView *)tableView
{
    
    if (tableView == self.renovateTableView) {
        
        [JQAFNetworkTool getUrl:@"http://api.meilijia.com/app/ba_diary_book_list?type=2&sort=&page=1" body:nil result:JQJSON headerFile:nil success:^(id result) {
            
            self.renovateArray = [NSMutableArray array];
            NSDictionary *dic = result[@"data"];
            self.renovateArray = [Team9RenovateModel baseModelByArr:dic[@"ba_diary_book_list"][@"list"]].mutableCopy;
            
            [self.renovateTableView reloadData];
            [self.renovateTableView.header endRefreshing];
            
        } failure:^(NSError *error) {
            
        }];

    }else{
    
    
        [JQAFNetworkTool getUrl:@"http://api.meilijia.com/app/showhome_list?page=1" body:nil result:JQJSON headerFile:nil success:^(id result) {
            
            self.homeArray = [NSMutableArray array];
            self.homeArray = [Team9HomeModel baseModelByArr:result[@"data"][@"showhome_list"][@"collections"]].mutableCopy;
            
            [self.homeTableView reloadData];
            [self.homeTableView.header endRefreshing];
            
        } failure:^(NSError *error) {
            
        }];

    
    }
    
    
    
}





- (void)loadMoreData:(UITableView *)tableView{
    
    static NSInteger i = 2;
    
    if (tableView == self.renovateTableView) {
        
        [JQAFNetworkTool getUrl:[NSString stringWithFormat:@"http://api.meilijia.com/app/ba_diary_book_list?type=2&sort=&page=%ld",i] body:nil result:JQJSON headerFile:nil success:^(id result) {
            
            NSMutableArray *arr = [NSMutableArray array];
            
            arr = [Team9RenovateModel baseModelByArr:result[@"data"][@"ba_diary_book_list"][@"list"]].mutableCopy;
            
            if (arr.count == 0) {
                [self.renovateTableView.footer endRefreshingWithNoMoreData];
            } else {
                [self.renovateTableView.footer endRefreshing];
            }
            [self.renovateArray addObjectsFromArray:arr];
            [self.renovateTableView reloadData];
            i+= 1;
            
            
        } failure:^(NSError *error) {
            [self.renovateTableView.footer endRefreshingWithNoMoreData];
        }];

        

    }else{
        
        [JQAFNetworkTool getUrl:[NSString stringWithFormat:@"http://api.meilijia.com/app/showhome_list?page=%ld",i] body:nil result:JQJSON headerFile:nil success:^(id result) {
            
            NSMutableArray *arr = [NSMutableArray array];
            
            arr = [Team9HomeModel baseModelByArr:result[@"data"][@"showhome_list"][@"collections"]].mutableCopy;
            
            if (arr.count == 0) {
                
                [self.homeTableView.footer endRefreshingWithNoMoreData];
                
            } else {
                
                [self.homeTableView.footer endRefreshing];
                
            }
            [self.homeArray addObjectsFromArray:arr];
            
            [self.homeTableView reloadData];
            
            i+= 1;
            
            
        } failure:^(NSError *error) {
            [self.renovateTableView.footer endRefreshingWithNoMoreData];
        }];

    
       
    
    
    }
    
  
    
    
}



#pragma mark 已晒家数据解析
- (void)createHomeData{
    
   self.tool = [Team9RefreshTool toolWithScrollView:self.homeTableView url:@"http://api.meilijia.com/app/showhome_list?page=1" style:^NSArray *(id result) {
        
        self.homeArray = [NSMutableArray array];
        return  [Team9HomeModel baseModelByArr:result[@"data"][@"showhome_list"][@"collections"]];
        
        
    } refresh:^(NSArray *array) {
        
        self.homeArray = array.mutableCopy;
        [self.homeTableView reloadData];
        
        
        
    } empty:self.empty2];
    
//
//    [JQAFNetworkTool getUrl:@"http://api.meilijia.com/app/showhome_list?page=1" body:nil result:JQJSON headerFile:nil success:^(id result) {
//        self.homeArray = [NSMutableArray array];
//        self.homeArray = [Team9HomeModel baseModelByArr:result[@"data"][@"showhome_list"][@"collections"]].mutableCopy;
//        [self.homeTableView reloadData];
//       
//        
//        
//        // 触发下拉刷新方法
//        self.homeTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//            
//            [self loadNewData:self.homeTableView];
//            
//        }];
//        
//        
//        // 触发上拉加载数据方法
//        self.homeTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//            
//            [self loadMoreData:self.homeTableView];
//            
//        }];
//        
//        
//        
////        [self.homeTableView reloadData];
//        
//        
//    } failure:^(NSError *error) {
//        
//        
//    }];

    
   }
#pragma mark 创建滑动scrollView
- (void) createScrollView{

    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 36 / 667.0* HEGHT , WIDTH , HEGHT -( 64 + 49 + 38) / 667.0 * HEGHT)];
    [self.view addSubview:self.scrollView];
    self.scrollView.contentSize = CGSizeMake(WIDTH * 2, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.delegate = self;
    
    
}




#pragma mark 创建装修中， 已晒家按钮
- (void)createHeaderView
{

    
  
    UIView *header = [[Team9BaseView alloc] init];
    header.frame = CGRectMake(0, 0, WIDTH, 30 / 375.0 * WIDTH);
    [self.view addSubview:header];
//    header.backgroundColor = [UIColor whiteColor];
    
    Team9BaseView *line = [[Team9BaseView alloc] initWithType:ViewTYpeLight];
    line.frame = CGRectMake(0, 30 / 375.0 * WIDTH, WIDTH, 5 / 667.0 * HEGHT);
    [self.view addSubview:line];
    
    
    
    self.renovateButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.renovateButton setTitle:@"装修中" forState:UIControlStateNormal];
    [header addSubview:self.renovateButton];
    [self.renovateButton addTarget:self action:@selector(clickLeft:) forControlEvents:UIControlEventTouchUpInside];
    self.renovateButton.titleLabel.font = [UIFont systemFontOfSize:15 / 375.0 * WIDTH];
    
    
    
    self.homeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.homeButton setTitle:@"已晒家" forState:UIControlStateNormal];
//    [self.homeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [header addSubview:self.homeButton];
    self.homeButton.titleLabel.font = [UIFont systemFontOfSize:15 / 375.0 * WIDTH];
    [self.homeButton addTarget:self action:@selector(clickRight:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.renovateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(header);
        make.right.equalTo(header.mas_centerX).offset(-5 / 375.0 * WIDTH);
    }];
    [self.homeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(header);
        make.left.equalTo(header.mas_centerX).offset(5 / 375.0 * WIDTH);
    }];
    
    self.lineView = [[UIView alloc] init];
    [self.view addSubview:self.lineView];
    if ([Team9EveningType shareEvening].eveningFailling) {
        self.lineView.backgroundColor = [UIColor greenColor];
    }else{
     self.lineView.backgroundColor = [UIColor orangeColor];
    }


    [self clickLeft:self.renovateButton];
}

- (void)clickLeft:(UIButton *)button{
    [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.renovateButton);
        make.height.mas_equalTo(1 / 375.0 * WIDTH);
         [self.lineView updateConstraintsIfNeeded];  // 黄线移动
    }];
    
    // 夜间变色
    if ([Team9EveningType shareEvening].eveningFailling) {
        [self.renovateButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        [self.homeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }else{
    
        [self.renovateButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [self.homeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    }
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    
    self.selectRight = NO;
}



- (void)clickRight:(UIButton *)button{
    
    [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.homeButton);
        make.height.mas_equalTo(1 / 375.0 * WIDTH);
        [self.lineView updateConstraintsIfNeeded];
    }];
    
    if ([Team9EveningType shareEvening].eveningFailling) {
        [self.renovateButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.homeButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];

    }else{
        [self.renovateButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.homeButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    
    }
    
    [self.scrollView setContentOffset:CGPointMake(WIDTH, 0) animated:YES];
    self.selectRight = YES;
}

#pragma mark  黄线随着左右滑动实时移动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == self.scrollView) {
        CGRect frame = self.lineView.frame;
        CGFloat left = self.renovateButton.frame.origin.x;
        CGFloat right = self.homeButton.frame.origin.x;
        CGFloat leftWidth = self.renovateButton.frame.size.width;
        CGFloat rightWidth = self.homeButton.frame.size.width;
        CGFloat proportion = scrollView.contentOffset.x / WIDTH;
        frame.origin.x = left + (right - left) * proportion;
        frame.size.width = leftWidth + (rightWidth - leftWidth) * proportion;
        self.lineView.frame = frame;

    }
}




#pragma mark 创建“装修中”tableView
- (void)createRenovateTableView{


    self.renovateTableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    self.renovateTableView.contentInset = UIEdgeInsetsMake(0, 0, (64 + 49 + 38) / 375.0 * WIDTH, 0);
    [self.scrollView addSubview:self.renovateTableView];
    
    self.renovateTableView.delegate = self;
    self.renovateTableView.dataSource =self;
    
    UIView *view = [[UIView alloc] init];
    self.renovateTableView.tableFooterView = view;
}

#pragma mark 创建“已晒家”tableView
- (void)createHomeTableView{
    self.homeTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HEGHT - (64 + 49 + 38) / 375.0 * WIDTH ) style:UITableViewStylePlain];
    [self.scrollView addSubview:self.homeTableView];
//    self.homeTableView.backgroundColor = [UIColor whiteColor];
    self.homeTableView.delegate = self;
    self.homeTableView.dataSource =self;
    
    
    UIView *view = [[UIView alloc] init];
    self.homeTableView.tableFooterView = view;

}

#pragma mark scrollView协议
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
   
    if (self.scrollView.contentOffset.x == 0) {
        [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.equalTo(self.renovateButton);
                make.height.mas_equalTo(1 / 375.0 * WIDTH);
        }];
        self.selectRight = NO;
        
    } else if (self.scrollView.contentOffset.x == WIDTH){
         [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.equalTo(self.homeButton);
                make.height.mas_equalTo(1 / 375.0 * WIDTH);
    }];
        self.selectRight = YES;
}
    
    [self changecolor];
    
    
}

- (void) changecolor{
    [super changecolor];
    if ([Team9EveningType shareEvening].eveningFailling) {
        self.lineView.backgroundColor = [UIColor greenColor];
        if (self.selectRight) {
            [self.renovateButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [self.homeButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        } else {
            [self.renovateButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
            [self.homeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
    } else{
        self.lineView.backgroundColor = [UIColor orangeColor];
        if (self.selectRight){
            [self.renovateButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [self.homeButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        } else {
            [self.renovateButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
            [self.homeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
}

#pragma mark tableView协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.renovateTableView == tableView) {
        return self.renovateArray.count;
       
       
    }else{
        return self.homeArray.count;
        
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    

    
    if (tableView == self.renovateTableView) {
        
        Team9RenovateModel *renovateModel = self.renovateArray[indexPath.row];
        NSDictionary *fontDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15 / 375.0 * WIDTH], NSFontAttributeName, nil];
        CGRect rect = [renovateModel.diary_content boundingRectWithSize:CGSizeMake(WIDTH - 40 / 375.0 * WIDTH, 0) options: NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
        
        if (renovateModel.diary_pics.count == 0) {
            return rect.size.height + 100 / 375.0 * WIDTH + 10 / 375.0 * WIDTH;
        }else{
            return rect.size.height + 190 / 375.0 * WIDTH + 10 / 375.0 * WIDTH;
        }
        
        
    }else{
        return 300 / 375.0 * WIDTH;
    }

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (tableView == self.renovateTableView) {
        static NSString *reuse = @"reuse";
        Team9RenovateTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[Team9RenovateTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        }
        cell.vc = self.vc;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        Team9RenovateModel *renovateModel = self.renovateArray[indexPath.row];
        cell.renovateModel = renovateModel;
        return cell;
    }else{
    static NSString *reuse = @"Reuse";
        Team9homeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[Team9homeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.vc = self.vc;
        cell.homeModel = self.homeArray[indexPath.row];
      

       
        return cell;
    }
    

}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.renovateTableView) {
        Team9renovatePushViewController *pushVC = [[Team9renovatePushViewController alloc] init];
        pushVC.hidesBottomBarWhenPushed = YES;
        
        Team9RenovateModel *model = self.renovateArray[indexPath.row];
        pushVC.bookId = model.col_id;
        [self.navigationController pushViewController:pushVC animated:YES];
        
        
    }else{
        Team9s_homeViewController *s_homeVC = [[Team9s_homeViewController alloc] init];
        s_homeVC.hidesBottomBarWhenPushed = YES;
        
        Team9HomeModel *model = self.homeArray[indexPath.row];
        s_homeVC.modelId = model.modelId;
        [self.navigationController pushViewController:s_homeVC animated:YES];
    
        
        
       
    }

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
 }



@end
